//
//  ST_PairsLoveMultiST_PeculiarityCell.swift
//  btvideo
//
//  Created by mac on 2020/3/18.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

/**
 个人主页-喜欢特质
 */
class ST_PairsLoveMultiST_PeculiarityCell: UITableViewCell {

    static let itemViewWidth = (ScreenWidth - 15 * 2 - 5 * 3)/4.0
    
    static let itemViewHeight = (ScreenWidth - 15 * 2 - 5 * 3)/4.0 + 47
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        
        contentView.backgroundColor = UIColor.init(hex: 0xACB4C0)
        
        contentView.addSubview(corner_radius_view)
        corner_radius_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 3, left: 0, bottom: 0, right: 0))
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(25)
        }
        var lastView:PairsOwnST_PeculiarityCellItem?
        var oneLineLastView:PairsOwnST_PeculiarityCellItem?
        for i in 0...7{
            let itemView = PairsOwnST_PeculiarityCellItem()
            self.itemViews.append(itemView)
            contentView.addSubview(itemView)
            
            if i == 4{
                lastView = nil
            }
            
            itemView.snp.makeConstraints { (make) in
                if let last = lastView{
                    make.left.equalTo(last.snp.right).offset(5)
                }else{
                    make.left.equalToSuperview().offset(15)
                }
                if let last = oneLineLastView{
                    make.top.equalTo(last.snp.bottom).offset(10)
                }else{
                    make.top.equalToSuperview().offset(61)
                }
                make.width.equalTo(ST_PairsLoveMultiST_PeculiarityCell.itemViewWidth)
                make.height.equalTo(ST_PairsLoveMultiST_PeculiarityCell.itemViewHeight)
            }
            
            lastView = itemView
            
            if i == 3{
                oneLineLastView = itemView
            }
            
        }
        
        contentView.addSubview(more_data_btn)
        more_data_btn.snp.makeConstraints { (make) in
            make.width.equalTo(140)
            make.height.equalTo(35)
            make.bottom.equalToSuperview().offset(-25)
            make.centerX.equalToSuperview()
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate var itemViews = [PairsOwnST_PeculiarityCellItem]()

    
    fileprivate lazy var corner_radius_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 30
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "  "
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var more_data_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.cornerRadius = 35/2.0
        control.setTitleForAllStates("查看更多")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(more_data_handle), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()
    
//    MARK: - Bind Model
    
    var model = ST_PairsLovePeculiaritySetsModel(){
        didSet{
            
            more_data_btn.isHidden = !self.model.showMoreDataBtn
          
            for (idx,item) in itemViews.enumerated(){
                
                
                item.ta = self.model.ta
                
                if let m = self.model.data_array[safe:idx]{
                    
                    item.isHidden = false
                    
                    item.model = m
                    
                }else{
                    item.isHidden = true
                }
            }
            titleLabel.text = model.ta + "喜欢的特质"
        }
    }
    
    @objc func more_data_handle(){
        
        let vc = ST_LikedPeculiarityViewController()
        
        vc.ta = self.model.ta
        
        vc.accid = self.model.accid
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
    
}





/**
 个人主页- 我喜欢特质 没有我符合
 */
class PairsMyLoveMultiST_PeculiarityCell: UITableViewCell {

    static let itemViewWidth = (ScreenWidth - 15 * 2 - 5 * 3)/4.0
    
    static let itemViewHeight = (ScreenWidth - 15 * 2 - 5 * 3)/4.0 + 47 - 15
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        contentView.backgroundColor = UIColor.init(hex: 0xACB4C0)
        
        contentView.addSubview(corner_radius_view)
        corner_radius_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 3, left: 0, bottom: 0, right: 0))
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(25)
        }
        var lastView:PairsOwnST_PeculiarityCellItem?
        var oneLineLastView:PairsOwnST_PeculiarityCellItem?
        for i in 0...7{
            let itemView = PairsOwnST_PeculiarityCellItem()
            self.itemViews.append(itemView)
            contentView.addSubview(itemView)
            
            if i == 4{
                lastView = nil
            }
            
            itemView.snp.makeConstraints { (make) in
                if let last = lastView{
                    make.left.equalTo(last.snp.right).offset(5)
                }else{
                    make.left.equalToSuperview().offset(15)
                }
                if let last = oneLineLastView{
                    make.top.equalTo(last.snp.bottom).offset(10)
                }else{
                    make.top.equalToSuperview().offset(61)
                }
                make.width.equalTo(PairsMyLoveMultiST_PeculiarityCell.itemViewWidth)
                make.height.equalTo(PairsMyLoveMultiST_PeculiarityCell.itemViewHeight)
            }
            
            lastView = itemView
            
            if i == 3{
                oneLineLastView = itemView
            }
            
        }
        
        contentView.addSubview(more_data_btn)
        more_data_btn.snp.makeConstraints { (make) in
            make.width.equalTo(140)
            make.height.equalTo(35)
            make.bottom.equalToSuperview().offset(-25)
            make.centerX.equalToSuperview()
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate var itemViews = [PairsOwnST_PeculiarityCellItem]()

    
    fileprivate lazy var corner_radius_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 30
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "  "
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var more_data_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.cornerRadius = 35/2.0
        control.setTitleForAllStates("查看更多")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(more_data_handle), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()
    
//    MARK: - Bind Model
    
    var model = ST_PairsLovePeculiaritySetsModel(){
        didSet{
            
            more_data_btn.isHidden = !self.model.showMoreDataBtn
          
            for (idx,item) in itemViews.enumerated(){
                
                if let m = self.model.data_array[safe:idx]{
                    
                    item.isHidden = false
                    
                    item.model = m
                    item.ta = model.ta
                }else{
                    item.isHidden = true
                }
            }
            titleLabel.text = model.ta + "喜欢的特质"
        }
    }
    
    @objc func more_data_handle(){
        
        let vc = ST_LikedPeculiarityViewController()
        
        vc.ta = self.model.ta
        
        vc.accid = self.model.accid
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
    
}

//MARK: - item View
fileprivate class PairsOwnST_PeculiarityCellItem: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(icon_image_view.snp.width)
        }
        
        self.addSubview(peculiarityTextLabel)
        peculiarityTextLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon_image_view.snp.bottom).offset(9)
        }
        
     
        self.addSubview(right_sign_view)
        right_sign_view.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.width.equalTo(45+20)
            make.centerX.equalToSuperview()
            make.top.equalTo(peculiarityTextLabel.snp.bottom).offset(2)
        }
        
        right_sign_view.addSubview(right_sign_text_label)
        right_sign_text_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        right_sign_view.addSubview(right_sign_corner_view)
        right_sign_corner_view.snp.makeConstraints { (make) in
            make.right.equalTo(right_sign_text_label.snp.left).offset(-5)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(6)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_pec_handle))
        
        self.addGestureRecognizer(tap)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//        MARK:-Properties
//
//    lazy var regimentation_label:UILabel = {
//        let control = UILabel.init()
//
//        control.textColor = Color.init(hexString: "#ACB4C0")
//        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
//
//        return control
//    }()
//
    lazy var peculiarityTextLabel:UILabel = {
        let control = UILabel.init()
        
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        
        return control
    }()

    lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 10
        return control
    }()
    
    
    fileprivate lazy var right_sign_view:UIView = {
       let control = UIView()
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var right_sign_corner_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.init(hex: 0x42DAA3)
        control.cornerRadius = 3
        return control
    }()
    
    fileprivate lazy var right_sign_text_label:UILabel = {
       let control = UILabel()
        control.textColor = Color.init(hex: 0xACB4C0)
        control.text = "我符合！"
        control.font = UIFont.systemFont(ofSize: 12)
        return control
    }()
    
    
//    MARK:- Bind Model
    var model = ST_PairsPeculiarity(){
        didSet{
            
            
            icon_image_view.pin_setImage(from: model.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            peculiarityTextLabel.text = model.name
            
            right_sign_view.isHidden = !model.is_conform
            
        }
    }
    
    var ta = ""
    
    @objc fileprivate func push_pec_handle(){
        
        var sameGender = true
        //     自己的话 这个地方展示异性特质
        if ta.contains("我"){
            sameGender = false
        }else if ta.contains("他") && ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male{
            sameGender = false
        }else if ta.contains("她") && ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .female{
            sameGender = false
        }
        
        push_peculiarity(id: model.id, onlyDisplayPerson: sameGender, navigationController: self.parentViewController?.navigationController)
        
    }
    
    

    
}
